//
// Created by Ge on 2024/3/5.
//
#include "include/SimpleCube.h"
#include "include/ShareLoader.h"
#include <GLES2/gl2.h>
#include "include/glm/glm.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/glm/gtc/type_ptr.hpp"
#include "include/LogUts.h"
void SimpleCube::draw() {
    GLuint vertexShaderHandle = loadShader(GL_VERTEX_SHADER, vertexShaderCode);
    GLuint fragmentShaderHandle = loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
    //创建着色器程序
    GLuint shaderProgramHandle = glCreateProgram();
    glAttachShader(shaderProgramHandle, vertexShaderHandle);
    glAttachShader(shaderProgramHandle, fragmentShaderHandle);
    //链接着色器程序
    glLinkProgram(shaderProgramHandle);
    // 获取着色器程序链接成功与否
    GLint linkSuccess;
    glGetProgramiv(shaderProgramHandle, GL_LINK_STATUS, &linkSuccess);
    if (linkSuccess == GL_FALSE) {
        glDeleteProgram(shaderProgramHandle);
        LOGE("着色器程序链接失败");
        return;
    }
    //使用着色器程序
    glUseProgram(shaderProgramHandle);
    GLuint vPositionLoc = glGetAttribLocation(shaderProgramHandle, "vPosition");
    glEnableVertexAttribArray(vPositionLoc);
    glVertexAttribPointer(vPositionLoc, 3,
                          GL_FLOAT, GL_FALSE,
                          0, vertices);
    glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(indices[0]), GL_UNSIGNED_SHORT, indices);
    // 释放资源
    glDisableVertexAttribArray(vPositionLoc);
    glDetachShader(shaderProgramHandle, vertexShaderHandle);
    glDetachShader(shaderProgramHandle, fragmentShaderHandle);
    glDeleteProgram(shaderProgramHandle);
    glDeleteShader(vertexShaderHandle);
    glDeleteShader(fragmentShaderHandle);
}
